package com.smt.modules.bus.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.smt.common.exception.RRException;
import com.smt.modules.bus.vo.AreaVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.smt.common.utils.PageUtils;
import com.smt.common.utils.Query;

import com.smt.modules.bus.dao.AreaDao;
import com.smt.modules.bus.entity.AreaEntity;
import com.smt.modules.bus.service.AreaService;


@Service("areaService")
public class AreaServiceImpl extends ServiceImpl<AreaDao, AreaEntity> implements AreaService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<AreaEntity> page = this.selectPage(
                new Query<AreaEntity>(params).getPage(),
                new EntityWrapper<AreaEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<AreaVo> queryArea(Integer levelType,Long parentId){

        Wrapper wrapper = new EntityWrapper();
        if (levelType != null){
            wrapper.eq("level_type",levelType);
        }
        if (parentId == null){
            throw new RRException("parentId必填！");
        }
        wrapper.eq("parent_id",parentId);
        List<AreaEntity> areaEntities = baseMapper.selectList(wrapper);

        areaEntities.stream()
                .forEach(e -> {
                    if(e.getLevelType() == 3){
                        e.setArrays(null);
                    }else{
                        String[] arrayStr = new String[]{};
                        e.setArrays(arrayStr);
                    }
                });

        return JSONObject.parseArray(JSONObject.toJSONString(areaEntities),AreaVo.class);
    }

}
